﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using QWEntity.Local;
using QWModel;
using QWRepository;
using QWRepository.Local;
using QWTouchPanel.Properties;

namespace QWTouchPanel.Model
{
    /// <summary>
    /// QKasirWidget : UserControl
    /// </summary>
    public partial class QWKasirPrint : UserControl
    {

        private bool _stillProcessing;

        /// <summary>
        /// Initializes a new instance of the <see cref="QWKasirPrint"/> class.
        /// </summary>
        public QWKasirPrint()
        {
            InitializeComponent();

            //var t = new Thread(TBackgroundImage);
            //t.Start();

            Parallel.Invoke(TBackgroundImage);
            _stillProcessing = false;

        }

        /// <summary>
        /// Ts the background image.
        /// </summary>
        private void TBackgroundImage()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TBackgroundImage);
                Invoke(o);
            }
            else
            {
                BackgroundImage = Resources.widget_black480;
            }
        }

        /// <summary>
        /// Handles the MouseUp event of the qwButton1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwButton1_MouseUp(object sender, MouseEventArgs e)
        {

            if (_stillProcessing) return;

            _stillProcessing = true;

            Layanan r = new LayananBase().GetById("B");

            if (r == null) return;

            // lock cek
            if (r.IsLocked)
            {

                var tLocked = new Thread(TLocked);
                tLocked.Start();
                Thread.Sleep(100);

                _stillProcessing = false;

                return;
            }

            var t = new Thread(PrintNomorAntrianKodeB);
            t.Start();
            Thread.Sleep(250);

            _stillProcessing = false;

        }

        /// <summary>
        /// Handles the MouseUp event of the qwButton2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwButton2_MouseUp(object sender, MouseEventArgs e)
        {

            if (_stillProcessing) return;

            _stillProcessing = true;

            Layanan r = new LayananBase().GetById("C");

            if (r == null) return;

            // lock cek
            if (r.IsLocked)
            {
                var tLocked = new Thread(TLocked);
                tLocked.Start();

                Thread.Sleep(100);

                _stillProcessing = false;

                return;
            }

            var t = new Thread(PrintNomorAntrianKodeC);
            t.Start();
            Thread.Sleep(250);

            _stillProcessing = false;

        }

        /// <summary>
        /// Prints the nomor antrian kode C.
        /// </summary>
        private void PrintNomorAntrianKodeC()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(PrintNomorAntrianKodeC);
                Invoke(o);
            }
            else
            {
                PrintNomorAntrian("C");
            }
        }

        /// <summary>
        /// Prints the nomor antrian kode B.
        /// </summary>
        private void PrintNomorAntrianKodeB()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(PrintNomorAntrianKodeB);
                Invoke(o);
            }
            else
            {
                PrintNomorAntrian("B");
            }
        }

        /// <summary>
        /// Ts the locked.
        /// </summary>
        private void TLocked()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TLocked);
                Invoke(o);
            }
            else
            {
                using (var f = new QWaitingWidget("Maaf, layanan ini tidak aktif ! "))
                {
                    f.Show(this);
                    Application.DoEvents();
                    Thread.Sleep(1000);
                }
            }
        }

        /// <summary>
        /// Prints the antrian.
        /// </summary>
        /// <param name="layananid">The layananid.</param>
        private void PrintNomorAntrian(string layananid)
        {
            string session = EnvironmentRepository.GetLocalSession();

            var nomor = new Kasir
                            {
                                LayananID = layananid,
                                Session = session
                            };

            var db = new KasirBase();
            int nResult = db.SaveByDml(nomor);

            if (nResult == 1)
            {
                Kasir rs = db.GetById(session);

                try
                {
                    string nocetak =
                        string.Format("{0}-{1}", rs.LayananID,
                                      rs.LayananNomor.ToString().Trim().PadLeft(3, '0'));

                    /* ---------------------------
                     *  get copies configuration
                     * --------------------------- */
                    Layanan layanan = new LayananBase().GetById(layananid);

                    try
                    {
                        switch (Settings.Default.PrintingLayout)
                        {
                            case 0: // ESC
                                QWPrinting.PrintWithESC(nocetak, "KASIR-FOTO-WAWANCARA", layanan.Copies, this);
                                break;
                            case 1: // CR
                                QWPrinting.PrintWithCRLayout(nocetak, "KASIR-FOTO-WAWANCARA", layanan.Copies, this);
                                break;
                        }
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
                catch (NullReferenceException)
                {
                }
            }
        }

        #region Nested type: DelegateThis

        /// <summary>
        /// DelegateThis
        /// </summary>
        private delegate void DelegateThis();

        #endregion
    }
}